package com.mqtt.mqtt;

import android.content.Context;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.litepal.crud.LitePalSupport;

/**
 * @Author xiaowen
 * @Email xiaowen.wxf@google.com
 * @Date 2018/10/20 .
 * @Description This is MqttConnectionBean.
 */

public class MqttConnectionBean  extends LitePalSupport {

    private String clientId;
    private String userName;
    private String password;
    private String host;
    private String port;
    private String broker_ip;
    private String topic;
    private boolean isCleanSession = false;
    private int connectionTimeout = 10;
    private int keepAliveInterval = 20;
    private boolean isAutomaticReconnect = true;
    private int qos = 2;
    private boolean retained = false;

    public MqttConnectionBean(){

    }

    private MqttConnectionBean(Builder builder,Context context) {
        this.clientId = builder.clientId;
        this.userName = builder.userName;
        this.password = builder.password;
        this.host = builder.host;
        this.port = builder.port;
        this.broker_ip = "tcp://" + builder.host + ":" + builder.port;; //  return "tcp://" + this.host + ":" + port;
        this.topic = builder.topic;
        this.isCleanSession = builder.isCleanSession;
        this.connectionTimeout = builder.connectionTimeout;
        this.keepAliveInterval = builder.keepAliveInterval;
        this.isAutomaticReconnect = builder.isAutomaticReconnect;
        this.qos = builder.qos;
//        init(context);
    }

    public void init(Context context){
        MqttConnectOptions options = new MqttConnectOptions();
        //            // 设置连接的用户名
        options.setUserName(userName);
        // 设置连接的密码
        options.setPassword(password.toCharArray());
        // 设置超时时间 单位为秒
        options.setConnectionTimeout(connectionTimeout);
        // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线，但这个方法并没有重连的机制
        options.setKeepAliveInterval(keepAliveInterval);
        options.setAutomaticReconnect(isAutomaticReconnect);//断开后，是否自动连接
        options.setCleanSession(isCleanSession);//是否清空客户端的连接记录。若为true，则断开后，broker将自动清除该客户端连接信息

        MqttAndroidClient mqttAndroidClient = new MqttAndroidClient(context, this.getBroker_ip(), this.getClientId(), new MemoryPersistence());
    }

    public boolean isRetained() {
        return retained;
    }

    public void setRetained(boolean retained) {
        this.retained = retained;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getBroker_ip() {
        // tcp://192.168.1.157:61613
//        return "tcp://" + this.host + ":" + port;
        return broker_ip;
    }

    public void setBroker_ip(String broker_ip) {
        this.broker_ip = broker_ip;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public boolean isCleanSession() {
        return isCleanSession;
    }

    public void setCleanSession(boolean cleanSession) {
        isCleanSession = cleanSession;
    }

    public int getConnectionTimeout() {
        return connectionTimeout;
    }

    public void setConnectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    public int getKeepAliveInterval() {
        return keepAliveInterval;
    }

    public void setKeepAliveInterval(int keepAliveInterval) {
        this.keepAliveInterval = keepAliveInterval;
    }

    public boolean isAutomaticReconnect() {
        return isAutomaticReconnect;
    }

    public void setAutomaticReconnect(boolean automaticReconnect) {
        isAutomaticReconnect = automaticReconnect;
    }

    public int getQos() {
        return qos;
    }

    public void setQos(int qos) {
        this.qos = qos;
    }


    @Override
    public String toString() {
        return "MqttConnectionBean{" +
                "clientId='" + clientId + '\'' +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", host='" + host + '\'' +
                ", port='" + port + '\'' +
                ", broker_ip='" + getBroker_ip() + '\'' +
                ", topic='" + topic + '\'' +
                ", isCleanSession=" + isCleanSession +
                ", connectionTimeout=" + connectionTimeout +
                ", keepAliveInterval=" + keepAliveInterval +
                ", isAutomaticReconnect=" + isAutomaticReconnect +
                ", qos=" + qos +
                ", retained=" + retained +
                '}';
    }

    public static class Builder  extends LitePalSupport{

        private String clientId;
        private String userName;
        private String password;
        private String host;
        private String port;
        private String broker_ip;
        private String topic;
        private boolean isCleanSession = false;
        private int connectionTimeout = 10;
        private int keepAliveInterval = 20;
        private boolean isAutomaticReconnect = true;
        private int qos = 2;
        private boolean retained = false;

        public MqttConnectionBean build(Context context){
            return new MqttConnectionBean(this,context);
        }

        public Builder setRetained(boolean retained) {
            this.retained = retained;
            return this;
        }

        public Builder setClientId(String clientId) {
            this.clientId = clientId;
            return this;
        }

        public Builder setUserName(String userName) {
            this.userName = userName;
            return this;
        }

        public Builder setPassword(String password) {
            this.password = password;
            return this;
        }

        public Builder setHost(String host) {
            this.host = host;
            return this;
        }

        public Builder setPort(String port) {
            this.port = port;
            return this;
        }

        public Builder setBroker_ip(String broker_ip) {
            this.broker_ip = broker_ip;
            return this;
        }

        public Builder setTopic(String topic) {
            this.topic = topic;
            return this;
        }

        public Builder setCleanSession(boolean cleanSession) {
            isCleanSession = cleanSession;
            return this;
        }

        public Builder setConnectionTimeout(int connectionTimeout) {
            this.connectionTimeout = connectionTimeout;
            return this;
        }

        public Builder setKeepAliveInterval(int keepAliveInterval) {
            this.keepAliveInterval = keepAliveInterval;
            return this;
        }

        public Builder setAutomaticReconnect(boolean automaticReconnect) {
            isAutomaticReconnect = automaticReconnect;
            return this;
        }

        public Builder setQos(int qos) {
            this.qos = qos;
            return this;
        }

    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MqttConnectionBean)) return false;

        MqttConnectionBean that = (MqttConnectionBean) o;

        if (isCleanSession() != that.isCleanSession()) return false;
        if (getConnectionTimeout() != that.getConnectionTimeout()) return false;
        if (getKeepAliveInterval() != that.getKeepAliveInterval()) return false;
        if (isAutomaticReconnect() != that.isAutomaticReconnect()) return false;
        if (getQos() != that.getQos()) return false;
        if (isRetained() != that.isRetained()) return false;
        if (!getClientId().equals(that.getClientId())) return false;
        if (!getUserName().equals(that.getUserName())) return false;
        if (!getPassword().equals(that.getPassword())) return false;
        if (!getHost().equals(that.getHost())) return false;
        if (!getPort().equals(that.getPort())) return false;
        if (!getBroker_ip().equals(that.getBroker_ip())) return false;
        return getTopic().equals(that.getTopic());
    }

    @Override
    public int hashCode() {
        int result = getClientId().hashCode();
        result = 31 * result + getUserName().hashCode();
        result = 31 * result + getPassword().hashCode();
        result = 31 * result + getHost().hashCode();
        result = 31 * result + getPort().hashCode();
        result = 31 * result + getBroker_ip().hashCode();
        result = 31 * result + getTopic().hashCode();
        result = 31 * result + (isCleanSession() ? 1 : 0);
        result = 31 * result + getConnectionTimeout();
        result = 31 * result + getKeepAliveInterval();
        result = 31 * result + (isAutomaticReconnect() ? 1 : 0);
        result = 31 * result + getQos();
        result = 31 * result + (isRetained() ? 1 : 0);
        return result;
    }
}
